﻿using AutoMapper;
using Oliving.Edge.Common;
using Oliving.Edge.Dto.Oliving;
using Oliving.Edge.IRepository;
using Oliving.Edge.IServices.Oliving;
using Oliving.Edge.Model.Models;
using Oliving.Edge.Services.BASE;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Oliving.Edge.Services.Oliving
{



    public class Sys_configServices : BaseServices<sys_config>,
        ISys_configServices
    {
        #region 构造函数
        Isys_configRepository _dal;
        IMapper _mapper;
        INlog_Log_Repository _Nlog_Log_Repository;
        IRedisCacheManager _RedisCacheManager;
        public Sys_configServices(
            Isys_configRepository dal,
            IMapper mapper,
            INlog_Log_Repository Nlog_Log_Repository,
            IRedisCacheManager RedisCacheManager
            )
        {
            this._dal = dal;
            base.BaseDal = dal;
            this._mapper = mapper;
            this._Nlog_Log_Repository = Nlog_Log_Repository;
            this._RedisCacheManager = RedisCacheManager;
        }


        #endregion


        #region Get 



        public async Task<Sys_configDto> GetDto(int id)
        {
            sys_config model = await base.QueryById(id);

            // 注意就是这里,mapper
            var dto = _mapper.Map<Sys_configDto>(model);

            return dto;
        }

        public async Task<List<Sys_configDto>> GetList()
        {
            //var 
            List<sys_config> list = await base.Query(a => a.C_State != -1, a => a.C_ID);

            // 注意就是这里,mapper
            var dtos = _mapper.Map<List<Sys_configDto>>(list);

            return dtos;
        }



        #endregion


        [Caching(CacheOption = CachingOption.OPtionRemore)]
        public async Task<bool> UpdateDto(Sys_configDto dto)
        {

            #region MyRegion

            List<sys_config> testList = await _dal.EdgeGetList_MsSQL();

            List<Nlog_Log> db2List = await _Nlog_Log_Repository.EdgeGetList_DB2();


            #endregion



            #region MyRegion


            sys_config model = await base.QueryById(dto.C_ID);

            model.C_Key = dto.C_Key;
            model.C_Name = dto.C_Name;
            model.C_Renark = dto.C_Renark;
            model.C_State = dto.C_State;
            model.C_Type = dto.C_Type;
            model.C_Value = dto.C_Value;

            return await base.Update(model);

            #endregion



            #region MyRegion


            // 注意就是这里,mapper
            //var model = _mapper.Map<sys_config>(dto);
            //List<string> col = new List<string>();
            //List<string> colHL = new List<string>();
            //col.Add("C_Key"); col.Add("C_Name"); col.Add("C_Renark"); col.Add("C_Value");

            //return await base.Update(model, col, colHL, "");

            #endregion


        }

        public Task<int> AddDto(Sys_configDto dto)
        {
            throw new NotImplementedException();
        }

        [UseTran]
        public async Task<bool> Delete(int id)
        {
            if (id == 0)
            {
                throw new Exception("余额不足");
            }
            var r = await base.DeleteById(id);
            return r;
            //   
        }


        #region GetCache




        //[Caching(AbsoluteExpiration = 60)]
        public async Task<List<Sys_configDto>> GetListCache()
        {



            long a = this._RedisCacheManager.GetIncrement("EdgeIncrement");
            long b = this._RedisCacheManager.GetDecrement("GetDecrement");

            //Expression <Func<sys_config, bool>> queryWhere = x => true;
            Expression<Func<sys_config, bool>> whereExpression = w => true && (w.C_State != -1);

            //var queryWhere && 

            //  queryWhere

            // (a.C_State != -1)


            //whereExpression= whereExpression.


            List<sys_config> list = await base.Query(whereExpression, a => a.C_ID);

            // 注意就是这里,mapper
            var dtos = _mapper.Map<List<Sys_configDto>>(list);

            return dtos;
        }




        [Caching(AbsoluteExpiration = 60)]
        public async Task<Sys_configDto> GetDtoCache(int id)
        {
            sys_config model = await base.QueryById(id);

            // 注意就是这里,mapper
            var dto = _mapper.Map<Sys_configDto>(model);

            return dto;
        }




        #endregion

    }



}
